/**
 * 
 */
package cn.biplam.back.sql.dml;

import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;

/**
 * Insert语义模型
 * 简单模式用法：<BR>
 * <P>new Insert("表物理名称");</P>
 * <P>this.put(String 字段物理名称,String 字段值[包括必要的单引号]);</P>
 * <P>String this.getSimpleSql();</P>
 * 
 * @author 	WangSong 2009-6-11 上午10:04:49
 * @version 1.0
 */
public class Insert extends DmlBase{
	
	private StringBuilder sbFields;
	private StringBuilder sbValues;
	

	public Insert(String tableName) {
		super("Insert",tableName);
		this.sbFields=new StringBuilder();
		this.sbValues=new StringBuilder();
	}

	/**
	 * 直接成对传入Insert语句的字段和值(如果需要,值的单引号在外部创建好)
	 * @param sFieldName
	 * @param sFieldValue
	 * @throws ErrorMessage 
	 */
	public void putField(String sFieldName,String sFieldValue) throws ErrorMessage{
		if(this.dmlType==Dml_Type.Complex){
			throw new ErrorMessage("Complex Insert disallow put Simple fileds kv.");
		}
		this.sbFields.append(sFieldName);
		this.sbFields.append(',');
		this.sbValues.append(sFieldValue);
		this.sbValues.append(',');
	}
	
	/**
	 * 成对传入Insert语句的字段和值(值被套上单引号)
	 * @param sFieldName
	 * @param sFieldValue
	 * @throws ErrorMessage 
	 */
	public void putStringField(String sFieldName,String sFieldValue) throws ErrorMessage{
		if(this.dmlType==Dml_Type.Complex){
			throw new ErrorMessage("Complex Insert disallow put Simple fileds kv.");
		}
		this.sbFields.append(sFieldName);
		this.sbFields.append(",");
		this.sbValues.append("'");
		this.sbValues.append(sFieldValue);
		this.sbValues.append("',");
	}
	
	/**
	 * 直接传入Insert语句的字段和值,如果值为空则忽略该字段
	 * @param sFieldName
	 * @param sFieldValue
	 * @throws ErrorMessage
	 */
	public void putFieldIgnoreNull(String sFieldName,String sFieldValue) throws ErrorMessage{
		if(!TOOLS.StringIsNull(sFieldValue)){
			if(TOOLS.StringIsEmpty(sFieldValue)){
				this.putField(sFieldName, null);
			}else{
				this.putField(sFieldName, sFieldValue);
			}
		}
	}
	
	/**
	 * 直接传入Insert语句的字段和值,如果字段值为空,抛出异常
	 * @param sFieldName
	 * @param sFieldValue
	 * @throws ErrorMessage
	 */
	public void putFieldDenyNull(String sFieldName,String sFieldValue) throws ErrorMessage{
		if(TOOLS.StringIsNull(sFieldValue)){
			throw new ErrorMessage("Insert fields value is empty:"+sFieldName+".");
		}else{
			this.putField(sFieldName, sFieldValue);
		}
	}

	/**
	 * 传入Insert语句的字段和值,如果值为空则忽略该字段(值被套上单引号)
	 * @param sFieldName
	 * @param sFieldValue
	 * @throws ErrorMessage
	 */
	public void putStringFieldIgnoreNull(String sFieldName,String sFieldValue) throws ErrorMessage{
		if(!TOOLS.StringIsNull(sFieldValue)){
			sFieldValue = TOOLS.stringToDbDeal(sFieldValue);
			this.putStringField(sFieldName, sFieldValue);
		}
	}
	
	/**
	 * 传入Insert语句的字段和值,如果值为空则忽略该字段(值被套上单引号)
	 * @param sFieldName
	 * @param sFieldValue
	 * @throws ErrorMessage
	 */
	public void putStringFieldIgnoreNullAndEmpty(String sFieldName,String sFieldValue) throws ErrorMessage{
		if(!TOOLS.StringIsEmpty(sFieldValue)){
			sFieldValue = TOOLS.stringToDbDeal(sFieldValue);
			this.putStringField(sFieldName, sFieldValue);
		}
	}
	
	/**
	 * 传入Insert语句的字段和值,如果字段值为空,抛出异常(值被套上单引号)
	 * @param sFieldName
	 * @param sFieldValue
	 * @throws ErrorMessage
	 */
	public void putStringFieldDenyNull(String sFieldName,String sFieldValue) throws ErrorMessage{
		if(TOOLS.StringIsNull(sFieldValue)){
			throw new ErrorMessage("Insert fields value is empty:"+sFieldName+".");
		}else{
			sFieldValue = TOOLS.stringToDbDeal(sFieldValue);
			this.putStringField(sFieldName, sFieldValue);
		}
	}
	
	/**
	 * 由参数信息直接生成通用DML语句,忽略数据库版本
	 * @return
	 * @throws ErrorMessage 
	 */
	@Override
	public String getSimpleSql() throws ErrorMessage {
		if(this.dmlType==Dml_Type.Complex){
			throw new ErrorMessage("Complex Insert cannot get simple sql.");
		}
		this.sbFields.delete(sbFields.length()-1,sbFields.length());
		this.sbValues.delete(sbValues.length()-1,sbValues.length());
		StringBuilder sb = new StringBuilder();
		sb.append("insert into ");
		sb.append(this.sTableName);
		sb.append("(");
		sb.append(this.sbFields);
		sb.append(") values(");
		sb.append(this.sbValues);
		sb.append(")");
		return sb.toString();
	}
}
